<?php

namespace app\index\controller\hotel;

use app\admin\model\hotel\HotelOrder as HotelOrderModel;
use app\common\controller\Hotel;
use think\Db;
use think\Exception;
use think\exception\PDOException;

/**
 * 酒店订单
 *
 * @icon fa fa-circle-o
 */
class Refund extends Hotel
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';


    /**
     * HotelOrder模型对象
     * @var \app\admin\model\hotel\HotelOrderRefund
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\hotel\HotelOrderRefund;

    }



    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->with(['order'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $row) {
                $row->getRelation('order')->visible(['order_no']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function pass($ids = null)
    {

        try {
            $row = $this->model->get($ids);

            $order = HotelOrderModel::get($row->order_id);
            if(!$order)$this->error('订单不存在或被删除');

            $row->status = 1;
            $row->save();

            $order_model = new HotelOrderModel();
            $res = $order_model->refund($row->order_id,$row->realprice,$row->refundprice);

        }catch (\Exception $e) {

            $this->error($e->getMessage());
        }
        $this->success('退款成功');
    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        //$order_model = new HotelOrderModel();
        $order = HotelOrderModel::get($row->order_id);
        $order->order_status = 1;
        $order->order_status_name = '待入住';
        $order->salestate = 4;

        $res = $order->save();

        $row->status = 2;
        $res = $row->save();

        if ($res){
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
}